package top.huntwolf.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.annotation.URLControl;
import top.huntwolf.admin.annotation.URLMessage;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.pojo.CartItem;
import top.huntwolf.admin.service.CartService;

import java.util.List;
@URLControl
@Api(description = "购物车控制器")
@RestController
@CrossOrigin
@RequestMapping(value = "/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @URLMessage(url = "/cart/**")
    @PostMapping("")
    @ApiOperation("增加新购物车")
    public JsonResult addCartItem(@RequestBody List<CartItem> items){

        cartService.addItem(items);
        return JsonResult.success(null);

    }

    @DeleteMapping("/{itemId}")
    @ApiOperation("删除商品项")
    public JsonResult deleteCartItem(@PathVariable int itemId){

        cartService.deleteItem(itemId);

        return JsonResult.success(null);
    }

    @GetMapping("/items")
    @ApiOperation("查询所有购物车")
    public JsonResult<List<CartItem>> queryCartItems(){
        return JsonResult.success(cartService.queryCartItems());
    }

    @PutMapping("")
    @ApiOperation("更新购物车")
    public JsonResult updateBuyCart(@RequestBody List<CartItem> items){

        cartService.updateCartItems(items);
        return JsonResult.success(null);
    }

    @URLMessage(url = "/cart/clearItems")
    @DeleteMapping("/clearItems")
    @ApiOperation("清空购物车")
    public JsonResult clearItems(){
        cartService.clearItems();
        return JsonResult.success(null);
    }

}
